#include <iostream>
#include <memory>
#include "http.hpp"
#include "HttpProtocol.hpp"

// 服务器对业务的处理逻辑
std::string HandlerHttpRequest(std::string &request) // inbufferstream : "len\nx op y\n"
{
    HttpRequest httpreq;
    httpreq.Deserialize(request);
    httpreq.DebugHttp();

    std::string content = "<html><h1>hello bite</h1></html>";
    std::string httpstatusline = "Http/1.0 200 OK\r\n";
    std::string httpheader = "Content-Length: " + std::to_string(content.size()) + "\r\n";
    httpheader += "\r\n";

    std::string httpresponse = httpstatusline + httpheader + content;
    return httpresponse;
}
int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        std::cout << "Usage: " << argv[0] << " serverport" << std::endl;
        return 1;
    }

    uint16_t localport = std::stoi(argv[1]);
    std::unique_ptr<HttpServer> tvr(new HttpServer(localport, HandlerHttpRequest));
    tvr->Loop();

    return 0;
}